<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-row>
        <el-col :span="formColSpan">
          <el-form-item label="所属年份" prop="year" >
            <el-date-picker class="formItem"
              v-model="formData.year"
              value-format="YYYY"
              type="year"
              placeholder="选择所属年份"
            />
          </el-form-item>
        </el-col>
        <el-col :span="formColSpan" v-if="showMultiple">
          <el-form-item label="员工" prop="userIdS">
            <el-select v-model="formData.userIdS" placeholder="请选择员工" class="formItem" multiple collapse-tags>
              <el-option
                v-for="dict in userOptions"
                :key="dict.id"
                :label="dict.nickname"
                :value="dict.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="formColSpan" v-else>
          <el-form-item label="员工" prop="userId">
            <el-select v-model="formData.userId" placeholder="请选择员工" class="formItem">
              <el-option
                v-for="dict in userOptions"
                :key="dict.id"
                :label="dict.nickname"
                :value="dict.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="formColSpan">
          <el-form-item label="指标点" prop="performancePoint">
            <el-select v-model="formData.performancePoint" placeholder="请选择指标点" class="formItem">
              <el-option
                v-for="dict in getStrDictOptions(DICT_TYPE.CRM_PERFORMANCE_POINT)"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="formColSpan">
          <el-form-item label="1月份目标值" prop="targetValue1">
            <el-input-number v-model="formData.targetValue1" placeholder="请输入目标值" :controls="false" class="formItem"/>
          </el-form-item>
        </el-col>
        <el-col :span="formColSpan">
          <el-form-item label="2月份目标值" prop="targetValue2">
            <el-input-number v-model="formData.targetValue2" placeholder="请输入目标值" :controls="false" class="formItem"/>
          </el-form-item>
        </el-col>
        <el-col :span="formColSpan">
          <el-form-item label="3月份目标值" prop="targetValue3">
            <el-input-number v-model="formData.targetValue3" placeholder="请输入目标值" :controls="false" class="formItem"/>
          </el-form-item>
        </el-col>
        <el-col :span="formColSpan">
          <el-form-item label="4月份目标值" prop="targetValue4">
            <el-input-number v-model="formData.targetValue4" placeholder="请输入目标值" :controls="false" class="formItem"/>
          </el-form-item>
        </el-col>
        <el-col :span="formColSpan">
          <el-form-item label="5月份目标值" prop="targetValue5">
            <el-input-number v-model="formData.targetValue5" placeholder="请输入目标值" :controls="false" class="formItem"/>
          </el-form-item>
        </el-col>
        <el-col :span="formColSpan">
          <el-form-item label="6月份目标值" prop="targetValue6">
            <el-input-number v-model="formData.targetValue6" placeholder="请输入目标值" :controls="false" class="formItem"/>
          </el-form-item>
        </el-col>
        <el-col :span="formColSpan">
          <el-form-item label="7月份目标值" prop="targetValue7">
            <el-input-number v-model="formData.targetValue7" placeholder="请输入目标值" :controls="false" class="formItem"/>
          </el-form-item>
        </el-col>
        <el-col :span="formColSpan">
          <el-form-item label="8月份目标值" prop="targetValue8">
            <el-input-number v-model="formData.targetValue8" placeholder="请输入目标值" :controls="false" class="formItem"/>
          </el-form-item>
        </el-col>
        <el-col :span="formColSpan">
          <el-form-item label="9月份目标值" prop="targetValue9">
            <el-input-number v-model="formData.targetValue9" placeholder="请输入目标值" :controls="false" class="formItem"/>
          </el-form-item>
        </el-col>
        <el-col :span="formColSpan">
          <el-form-item label="10月份目标值" prop="targetValue10">
            <el-input-number v-model="formData.targetValue10" placeholder="请输入目标值" :controls="false" class="formItem"/>
          </el-form-item>
        </el-col>
        <el-col :span="formColSpan">
          <el-form-item label="11月份目标值" prop="targetValue11">
            <el-input-number v-model="formData.targetValue11" placeholder="请输入目标值" :controls="false" class="formItem"/>
          </el-form-item>
        </el-col>
        <el-col :span="formColSpan">
          <el-form-item label="12月份目标值" prop="targetValue12">
            <el-input-number v-model="formData.targetValue12" placeholder="请输入目标值" :controls="false" class="formItem" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import {  getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { PersonPerformanceConfigApi, PersonPerformanceConfig } from '@/api/crm/performance/person'
import {ElementPlusInfoType} from "@/types/elementPlus";
import * as UserApi from "@/api/system/user";

interface CommonDictDataType {
  dictType: string
  label: string
  value: string | number | boolean
  colorType: ElementPlusInfoType | ''
  cssClass: string
}
/** CRM_业绩配置 表单 */
defineOptions({ name: 'PerformanceConfigForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗
const formColSpan = ref(12)
const showMultiple = ref(true)

const userOptions = ref<UserApi.UserVO[]>([]) // 用户列表
const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  year: undefined,
  userId: undefined,
  userIdS: [],
  performancePoint: undefined,
  targetValue1: undefined,
  targetValue2: undefined,
  targetValue3: undefined,
  targetValue4: undefined,
  targetValue5: undefined,
  targetValue6: undefined,
  targetValue7: undefined,
  targetValue8: undefined,
  targetValue9: undefined,
  targetValue10: undefined,
  targetValue11: undefined,
  targetValue12: undefined,
})
const formRules = reactive({
  year: [
    { type: 'YEAR', required: true, message: '请选择所属年份', trigger: 'change' }
  ],
  userId: [
    { required: formType.value === 'update', message: '请填写员工', trigger: 'blur' }
  ],
  userIdS: [
    { required: formType.value === 'create', message: '请填写员工', trigger: 'blur' }
  ],
  performancePoint: [
    { required: true, message: '请填写指标点', trigger: 'blur' }
  ]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  showMultiple.value = true
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    showMultiple.value = false

    try {
      formData.value = await PersonPerformanceConfigApi.getPerformanceConfig(id)
    } finally {
      formLoading.value = false
    }
  }
  // 获得用户列表
  userOptions.value = await UserApi.getSimpleUserList()
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as PersonPerformanceConfig
    if (formType.value === 'create') {
      await PersonPerformanceConfigApi.createPerformanceConfig(data)
      message.success(t('common.createSuccess'))
    } else {
      await PersonPerformanceConfigApi.updatePerformanceConfig(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    year: undefined,
    userId: undefined,
    userIdS: [],
    performancePoint: undefined,
    targetValue1: undefined,
    targetValue2: undefined,
    targetValue3: undefined,
    targetValue4: undefined,
    targetValue5: undefined,
    targetValue6: undefined,
    targetValue7: undefined,
    targetValue8: undefined,
    targetValue9: undefined,
    targetValue10: undefined,
    targetValue11: undefined,
    targetValue12: undefined,
  }
  formRef.value?.resetFields()
}

/** 初始化 **/
onMounted(async () => {
  console.debug('person performance config')
})
</script>
<style scoped>
.formItem {
  width: 100%;
}
</style>
